package com.yiren.dbaa.module.project_edit.service.impl;

import com.yiren.backstage.currency.entity.JudgeDetailsEntity;
import com.yiren.backstage.currency.entity.JudgeEntity;
import com.yiren.backstage.currency.entity.MonitorCountEntity;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.entity.ProjectJudgeEntity;
import com.yiren.backstage.db.dao.cache.ProjectCache;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.dbaa.module.project_edit.service.IDataOperation;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @author lwb
 * @date : 2022/3/31 19:31
 * @description:  数据操作实现类
 */
public class DataOperationImpl implements IDataOperation {

    @Override
    public List<ProjectJudgeEntity> getProjectJudgeList() {
        List<ProjectJudgeEntity> projectJudgeList = GradeDbController.getInstance(false).queryAll(ProjectJudgeEntity.class);
        for (ProjectJudgeEntity projectJudge : projectJudgeList){
            projectJudge.setProject(ProjectCache.getPointById(projectJudge.getProjectId()));
        }
        return projectJudgeList;
    }

    @Override
    public List<ProjectEntity> getProjectList() {
        List<ProjectJudgeEntity> projectJudgeList = getProjectJudgeList();
        List<ProjectEntity> projectList = new ArrayList<>();
        for (ProjectJudgeEntity projectJudge : projectJudgeList) {
            projectList.add(projectJudge.getProject());
        }
        projectList = removeDuplicate(projectList);
        return projectList;
    }

    public static List<ProjectEntity> removeDuplicate(List<ProjectEntity> list) {
        Set set = new LinkedHashSet<ProjectEntity>();
        set.addAll(list);
        list.clear();
        list.addAll(set);
        return list;
    }

    @Override
    public boolean updateJudge(JudgeEntity judge) {
        return GradeDbController.getInstance(false).update(judge);
    }

    @Override
    public boolean updateJudgeDetail(JudgeDetailsEntity judgeDetails) {
        return GradeDbController.getInstance(false).update(judgeDetails);
    }

    @Override
    public boolean updateMonitorCount(MonitorCountEntity monitorCount) {
        return GradeDbController.getInstance(false).update(monitorCount);
    }

    @Override
    public boolean updateProject(ProjectEntity project) {
        return GradeDbController.getInstance(false).update(project);
    }


}
